package com.xdw.addressbookbyjava;

import com.xdw.addressbookbyjava.dao.ContactsDao;
import com.xdw.addressbookbyjava.dao.impl.ContactsDaoImpl;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.rpc.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONObject;

import java.util.HashMap;
import java.util.Map;

public class ContactsServiceAbility extends Ability {
    public static final String TAG = "ContactsServiceAbility";
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.DEBUG, 0xD001100, TAG);
    private ContactsDao contactsDao;  //声明数据库操作的dao接口
    @Override
    public void onStart(Intent intent) {
        HiLog.error(LABEL_LOG, "ContactsServiceAbility::onStart");
        super.onStart(intent);
        contactsDao = new ContactsDaoImpl();  //在服务启动的时候给dao接口初始化
    }

    @Override
    public void onBackground() {
        super.onBackground();
        HiLog.info(LABEL_LOG, "ContactsServiceAbility::onBackground");
    }

    @Override
    public void onStop() {
        super.onStop();
        HiLog.info(LABEL_LOG, "ContactsServiceAbility::onStop");
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        return remote.asObject();   //关键操作，服务连接的时候进行远程调用
    }

    @Override
    public void onDisconnect(Intent intent) {
    }

    private MyRemote remote = new MyRemote();   //定义远程调用对象
    // FA在请求PA服务时会调用AbilityconnectAbility连接PA，连接成功后，需要在onConnect返回一个remote对象，供FA向PA发送消息
    class MyRemote extends RemoteObject implements IRemoteBroker {
        private static final int ERROR = -1;    //定义数据请求错误码
        private static final int SUCCESS = 0;   //定义数据请求成功码
        private static final int FIND_ALL = 1001;           //查找所有联系人
        private static final int INSERT = 1002;           //添加联系人
        private static final int DELETE_BYID = 1003;           //删除联系人
        MyRemote() {
            super("MyService_MyRemote");
        }

        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
            switch (code) {
                case FIND_ALL: {
                    // 返回结果仅支持可序列化的Object类型，这里先查询数据库的数据，然后进行返回给JS UI
                    Map<String, Object> zsonResult = new HashMap<String, Object>();
                    zsonResult.put("code", SUCCESS);
                    zsonResult.put("abilityResult", contactsDao.getAllContacts());
                    reply.writeString(ZSONObject.toZSONString(zsonResult));
                    break;
                }
                case INSERT:    //添加联系人数据业务，待完成
                    break;
                case DELETE_BYID:   //删除联系人数据业务，待完成
                    break;
                default: {
                    reply.writeString("service not defined");
                    return false;
                }
            }
            return true;
        }

        @Override
        public IRemoteObject asObject() {
            return this;
        }
    }
}